package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.util.PageResult;
import com.atguigu.jxc.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ONIE
 * @create 2021/11/25 6:14 下午
 */

@RestController
@RequestMapping("supplier")
public class SupplierController {


	@Autowired
	private SupplierService supplierService;

	/**
	 * 分页查询供应商
	 * @param customerVo
	 * @return
	 */
	@PostMapping("list")
	public Map<String,Object> supplierList(SearchVo customerVo){
		Integer page = customerVo.getPage();
		Integer rows = customerVo.getRows();
		String supplierrName = customerVo.getSupplierName();
		if (page == null) page = 1;
		if (rows == null) rows = 5;
		PageResult pageResult = supplierService.supplierList(page,rows,supplierrName);
		HashMap<String, Object> map = new HashMap<>();
		map.put("total",pageResult.getTotal());
		map.put("rows",pageResult.getRows());
		return map;
	}

	/**
	 * 供应商添加或修改
	 * @param supplierId
	 * @param supplier
	 * @return
	 */
	@PostMapping("save")
	public ServiceVO save(@RequestParam(required = false) Integer supplierId , Supplier supplier){
		supplierService.saveBySupplierId(supplierId,supplier);
		return new ServiceVO(100,"请求成功");
	}


	/**
	 * 删除供应商
	 * @param ids
	 * @return
	 */
	@PostMapping("delete")
	public ServiceVO deleteByIds(@RequestParam String ids){
		supplierService.deleteByIds(ids);
		return new ServiceVO(100,"请求成功",null);
	}
}
